﻿using Microsoft.Extensions.DependencyInjection;
using System;
using Zocono.Infrastructure.Common;

namespace Zocono.WCS.HangfireHost.TaskJobs
{
    public class TaskLock
    {
        /// <summary>
        /// 心跳锁
        /// </summary>
        public bool HeartLock = false;

        /// <summary>
        /// 获取堆垛机任务作业锁
        /// </summary>
        public bool GetScTaskLock = false;

        /// <summary>
        /// 获取WMS任务作业锁
        /// </summary>
        public bool InitWmsTaskLock = false;

        /// <summary>
        /// 初始化PLC连接作业锁
        /// </summary>
        public bool InitPlcConnectTaskLock = false;

        /// <summary>
        /// 单次作业锁
        /// </summary>
        public bool OnceTaskLock = false;

        /// <summary>
        /// PLC重连作业锁
        /// </summary>
        public bool PlcReconnectTaskLock = false;


        private DateTime _DataToMenTaskLockUpdateTime = DateTime.Now;
        private bool _DataToMenTaskLock = false;
        /// <summary>
        /// WCS数据库数据存内存作业锁(获取监控数据)
        /// </summary>
        public bool DataToMenTaskLock
        {
            get
            {
                if (_DataToMenTaskLock && DateTime.Now.Subtract(_DataToMenTaskLockUpdateTime).TotalSeconds > 10)
                {
                    _DataToMenTaskLock = false;
                    var _LogUtil= ServiceCollectionExt.serviceProvider.GetService<ILogUtil>();
                    if (_LogUtil != null)
                        _LogUtil.Fatal("获取监控plc数据超时，重置锁");
                }
                return _DataToMenTaskLock;
            }
            set
            {
                _DataToMenTaskLock = value;
                _DataToMenTaskLockUpdateTime = DateTime.Now;
            }
        }
        public bool DataToMenTaskLockInvoking = false;


        /// <summary>
        /// 读取PLC作业锁
        /// </summary>
        public bool ReadPlcTaskLock = false;
    }
}
